package com.skt.common_hr.fudraw.view
{
	import com.skt.common_hr.common.Variables;
	import com.skt.common_hr.common.components.CommonAlert;
	import com.skt.common_hr.common.puremvc.CommonMediator;
	import com.skt.common_hr.fudraw.FudrawFacade;
	import com.skt.common_hr.fudraw.model.AttachFileProxy;
	import com.skt.common_hr.fudraw.model.NoticeViewProxy;
	import com.skt.common_hr.fudraw.model.NoticeWriteProxy;
	import com.skt.common_hr.fudraw.model.vo.NoticeDetailVO;
	import com.skt.common_hr.fudraw.model.vo.NoticeFileUpLoadVO;
	import com.skt.common_hr.fudraw.model.vo.NoticeVO;
	import com.skt.common_hr.fudraw.view.components.NoticeWrite;
	import com.wcl.utils.StringUtil;
	
	import flash.events.Event;
	
	import org.puremvc.as3.multicore.interfaces.INotification;

	public class NoticeWriteMediator extends CommonMediator
	{
		public static const NAME:String = "NoticeWriteMediator";

		private const MODE_WAITING:String = "modeWaiting";
		private const MODE_WRITING:String = "modeWriting";
		private const MODE_MODIFY:String = "modeModify";

		private var _mode:String = MODE_WAITING;
		private var _bWaitingSave:Boolean = false;
		private var orgFileName:String = "";

		public function NoticeWriteMediator(viewCompo:Object)
        {
            super( NAME, viewCompo );
        }

		override protected function initializeView():void
		{
			super.initializeView();
		}

		private function setEventListener():void
		{
			view.addEventListener(NoticeWrite.NoticeSave, onNoticeSave);
			view.addEventListener(NoticeWrite.NoticeOnlyFileDelete, onNoticeOnlyFileDelete);
			view.addEventListener(NoticeWrite.NoticeDelete, onNoticeDelete);
			view.addEventListener(NoticeWrite.NoticeCancel, onCancel);
			view.addEventListener(NoticeWrite.GetFile, onBrowseFile);
		}

		private function removeEventListener():void
		{
			view.removeEventListener(NoticeWrite.NoticeSave, onNoticeSave);
			view.removeEventListener(NoticeWrite.NoticeOnlyFileDelete, onNoticeOnlyFileDelete);
			view.removeEventListener(NoticeWrite.NoticeDelete, onNoticeDelete);
			view.removeEventListener(NoticeWrite.NoticeCancel, onCancel);
			view.removeEventListener(NoticeWrite.GetFile, onBrowseFile);
		}

		override protected function clearView():void
		{
			view.dataProvider = null;
		}

		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Process MVC Messages
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		* 본 Class에서 처리해야할 Noti들을 등록한다.
		*
		* @param
		* @return void
		*/
		override public function listNotificationInterests():Array
		{
			return [
					FudrawFacade.MODE_WRITE_NOTICE,
					FudrawFacade.MODE_MODIFY_NOTICE,
					FudrawFacade.INSERT_NOTICE_COMPLETE,
					FudrawFacade.INSERT_NOTICE_FAILED,
					FudrawFacade.UPDATE_NOTICE_COMPLETE,
					FudrawFacade.UPDATE_NOTICE_FAILED,
					AttachFileProxy.SELECTED_FILE,
					AttachFileProxy.PROGRESS_UPLOAD,
					AttachFileProxy.FAILED_UPLOAD,
					AttachFileProxy.COMPLETE_UPLOAD
					];
		}



		/**
		* Noti별로 처리한다.
		*
		* @param
		* @return void
		*/
		override public function handleNotification(note:INotification):void
		{
			switch(note.getName())
			{
				case FudrawFacade.MODE_WRITE_NOTICE:
					startWriting();
					break; 
				case FudrawFacade.MODE_MODIFY_NOTICE:
					startModify(note.getBody() as NoticeVO);
					break;
				case FudrawFacade.INSERT_NOTICE_COMPLETE:
					stopWriting();
					break;
				case FudrawFacade.INSERT_NOTICE_FAILED:
					CommonAlert.showMessage(commonFacade.bundle, "ALERT_ERROR_SAVE");
					break;

				case FudrawFacade.UPDATE_NOTICE_COMPLETE:
					stopWriting();
					break;
				case FudrawFacade.UPDATE_NOTICE_FAILED:
					CommonAlert.showMessage(commonFacade.bundle, "ALERT_ERROR_SAVE");
					break;

				case AttachFileProxy.SELECTED_FILE:
					onSelectedFile();
					break;
				case AttachFileProxy.PROGRESS_UPLOAD:
					onProgressUpload(note.getBody() as AttachFileProxy);
					break;
				case AttachFileProxy.COMPLETE_UPLOAD:
					onCompleteUpload(note.getBody() as AttachFileProxy);
					break;
				case AttachFileProxy.FAILED_UPLOAD:
					onFailedUpload(note.getBody() as AttachFileProxy);
					break;
			}
		}


		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	MVC Message handlers
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		private function startWriting():void
		{
			_mode = MODE_WRITING;
			setEventListener();
			orgFileName = "";
			view.dataProvider = null;
		}



		private function startModify(vo:NoticeVO):void
		{
			_mode = MODE_MODIFY;
			setEventListener();
			orgFileName = vo.FILE_NAME;
			view.dataProvider = vo;
		}

		private function stopWriting():void
		{
			_mode = MODE_WAITING;
 			removeEventListener();
 			clearView();
 			facade.sendNotification(FudrawFacade.MODE_LIST_NOTICE, true);
		}


		private function onSelectedFile():void
		{
			var proxy:AttachFileProxy = facade.retrieveProxy(AttachFileProxy.NAME) as AttachFileProxy;
			view.fileName = proxy.fileVo.I_FILE_NAME;
		}

		private function onProgressUpload(proxy:AttachFileProxy):void
		{
			trace("onProgressUpload: " + proxy.bytesLoaded);
		}

		private var fileInfo:NoticeFileUpLoadVO;

		private function onCompleteUpload(proxy:AttachFileProxy):void
		{
			trace("onCompleteUpload: " + proxy.fileVo.I_FILE_URL + "/" + proxy.fileVo.I_FILE_NAME);
			fileInfo = proxy.fileVo;
			if(Variables.company == "NOS") {
				fileInfo.I_FILE_URL = fileInfo.I_FILE_URL.toUpperCase();
			}
			if(_bWaitingSave)
				saveNotice();
		}

		private function onFailedUpload(proxy:AttachFileProxy):void
		{
			trace("onFailedUpload: " + proxy.fileVo.I_FILE_NAME);
			CommonAlert.showMessage(commonFacade.bundle, "ALERT_ERROR_UPLOAD");
			fileInfo = null;
		}








		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event handler (from View, Internal or....)
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		private function onCancel(e:Event):void
		{
			trace("onCancel");
			removeEventListener();
			facade.sendNotification(FudrawFacade.MODE_LIST_NOTICE, false);
		}


		private function buildSaveData():NoticeDetailVO
		{
			var vo:NoticeDetailVO = new NoticeDetailVO();

 			vo.I_BOARD_ID 		= "06"
 			vo.CONTENT_IDX		= view.writeKey;
 			trace(view.writeKey);
 			vo.I_TITLE			= view.titleNm.text;
 			vo.I_CONTENT		= view.content.htmlText;
 			vo.I_WRITER_PERNR	= Variables.myPernr;
 			//vo.I_BEGDA			= "";
 			//vo.I_ENDDA			= "";
 			vo.I_BEGDA			= view.Ibegda.text;
 			vo.I_ENDDA			= view.Iendda.text;

 			if(fileInfo!=null) {
	 			vo.I_FILE_NAME		= fileInfo.I_FILE_NAME;
	 			vo.I_FILE_EXT		= fileInfo.I_FILE_EXT;
	 			vo.I_FILE_URL		= fileInfo.I_FILE_URL;
	 			vo.I_FILE_SIZE		= fileInfo.I_FILE_SIZE.toString();
	 		}
 			return vo;
		}

 		//private var vo:NoticeDetailVO = new NoticeDetailVO();
 		private function onNoticeSave(event:Event):void
 		{
 			if(!validateFields()) {
 				return;
 			}
 			if(StringUtil.NVL(view.fileName)!="" && StringUtil.NVL(view.fileName)!=StringUtil.NVL(orgFileName)) {
 				_bWaitingSave = true;
				var proxy:AttachFileProxy = facade.retrieveProxy(AttachFileProxy.NAME) as AttachFileProxy;
				proxy.uploadFile();
 			}
 			else {
 				if(StringUtil.NVL(orgFileName)!="" && StringUtil.NVL(view.fileName)!=StringUtil.NVL(orgFileName)) {
 					deleteFile();
 				}
 				saveNotice();
 			}
 		}

		private function saveNotice():void
		{
			_bWaitingSave = false;
			var proxy:NoticeWriteProxy = facade.retrieveProxy(NoticeWriteProxy.NAME) as NoticeWriteProxy;
 			if(_mode == MODE_WRITING) {
	 			proxy.insert(buildSaveData());
 			}
 			else {
				proxy.update(buildSaveData());
 			}
		}
		private function onBrowseFile(event:Event):void
		{
			var proxy:AttachFileProxy = facade.retrieveProxy(AttachFileProxy.NAME) as AttachFileProxy;
			proxy.browse();
		}


 		private function onNoticeDelete(event:Event):void
 		{
			var param:Object = new Object();
  			param.I_BOARD_ID 		= "06"
 			param.CONTENT_IDX		= view.writeKey;

            //파일삭제
            //deleteFile();

 			var proxy:NoticeViewProxy = facade.retrieveProxy(NoticeViewProxy.NAME) as NoticeViewProxy;
			proxy.deleteData(param);
 		}

		private function deleteFile():void
		{

		}
		//파일삭제
		private function onNoticeOnlyFileDelete(event:Event):void
		{
			view.fileName = "";
			if(fileInfo) {
				fileInfo.I_FILE_EXT = "";
				fileInfo.I_FILE_NAME = "";
				fileInfo.I_FILE_SIZE = 0;
				fileInfo.I_FILE_URL = "";
			}

		}


		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Utility Functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		private function validateFields():Boolean
		{
			if(view.titleNm.text == "") {
				CommonAlert.showMessage("Notice", "ALERT_NO_TITLE");
				return false;
			}
			if(view.content.text == "") {
				CommonAlert.showMessage("Notice", "ALERT_NO_CONTENT");
				return false;
			}
			return true;
		}

        private function get view():NoticeWrite
        {
            return viewComponent as NoticeWrite;
        }
	}
}